package com.tgy.leetcode;

/**
 * @Author: tgy
 * @Date: 2020-11-25 09:28
 */
public class _75_颜色分类_01 {

    public void sortColors(int[] nums) {

        if (nums == null || nums.length <= 1) {

            return;
        }


        int left = 0, index = 0,right = nums.length - 1;

        while (index <= right) {

            if (nums[index] == 0) {

                swap(nums,left++,index++);
            }else if (nums[index] == 2) {

                swap(nums,index,right--);
            }else {

                index++;
            }
        }

    }

    private void swap(int[] nums,int i1,int i2) {

        if (i1 == i2) {

            return;
        }

        int tmp = nums[i1];
        nums[i1] = nums[i2];
        nums[i2] = tmp;

    }
}
